import allure,os
from datetime import datetime

def clean_screenshot_folder():
    '''
    清理Image文件夹中的所有图片
    '''
    image_folder = os.path.join(os.getcwd(), 'Image')
    if os.path.exists(image_folder):
        for filename in os.listdir(image_folder):
            file_path = os.path.join(image_folder, filename)
            try:
                if os.path.isfile(file_path) and filename.lower().endswith('.png'):
                    os.unlink(file_path)
            except Exception as e:
                print(f"删除文件 {file_path} 时出错: {e}")

def allure_tittle(tittle_feature,tittle_story,tittle_tittle):
    '''
    设置allure标题的方法
    :param tittle_feature: 传入feature级别标题
    :param tittle_story: 传入story级别标题
    :param tittle_tittle: 传入tittle级别标题
    :return:
    '''
    # 动态设置allure标题
    allure.dynamic.feature(tittle_feature)
    allure.dynamic.story(tittle_story)
    allure.dynamic.title(tittle_tittle)

def allure_screenshot(driver):
    '''
    allure截图并上传到报告的方法
    :param driver:传浏览器driver
    :return:
    '''
    # 生成当前时间，作为png文件名
    current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
    screenshot_path = os.path.join(os.getcwd(), 'Image', f'{current_time}.png')  # 保存截图的路径
    driver.save_screenshot(screenshot_path)  # 截图
    # 附加截图到报告
    allure.attach.file(screenshot_path, name=current_time, attachment_type=allure.attachment_type.PNG)

# 在模块导入时自动清理截图文件夹
clean_screenshot_folder()